package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.widget.swipelayout.SimpleSwipeListener;
import com.hjq.widget.swipelayout.SwipeLayout;
import com.hjq.widget.swipelayout.adapters.RecyclerSwipeAdapter;
import com.qiantu.api.entity.SwitchBindBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.bean.UISwitchBindBean;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class SwitchBindAdapter extends AppAdapter<UISwitchBindBean> {
    private Callback callback;

    public SwitchBindAdapter(@NonNull Context context, Callback callback) {
        super(context);
        this.callback = callback;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    public void removeSwitchBindBean(String switchBindSerialNo) {
        for (int i = 0; i < getCount(); i++) {
            for (int j = 0; j < getItem(i).getSwitchBindBeans().size(); j++) {
                if (getItem(i).getSwitchBindBeans().get(j).getSwitchBindSerialNo().equals(switchBindSerialNo)) {
                    getItem(i).getSwitchBindBeans().remove(getItem(i).getSwitchBindBeans().get(j));
                    notifyItemChanged(i);//更新当前ITEM
                    return;//结束循环
                }
            }
        }
    }

    public final class ViewHolder extends AppAdapter<?>.ViewHolder {
        private final TextView switchBindTitle;
        private final RecyclerView itemListView;

        private ViewHolder() {
            super(R.layout.item_switch_bind);
            switchBindTitle = findViewById(R.id.switch_bind_item_title);
            itemListView = findViewById(R.id.switch_list_view);
        }

        @Override
        public void onBindView(int position) {
            UISwitchBindBean item = getItem(position);
            switchBindTitle.setText(item.getBindActionName());
            itemListView.setAdapter(new SwitchItemAdapter(getContext(), item.getSwitchBindBeans(), callback));
            itemListView.setLayoutManager(new LinearLayoutManager(getContext()));
        }
    }

    public static final class SwitchItemAdapter extends RecyclerSwipeAdapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_switch_bind_sub, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
            ItemViewHolder vh = (ItemViewHolder) viewHolder;
            SwitchBindBean item = dataList.get(position);
            vh.btnDelete.setTag(item);
            vh.btnDelete.setOnClickListener(mOnClickListener);
            vh.swipeLayout.addSwipeListener(new SimpleSwipeListener() {
                @Override
                public void onOpen(SwipeLayout layout) {
                    viewHolder.setIsRecyclable(false);
                }

                @Override
                public void onClose(SwipeLayout layout) {
                    viewHolder.setIsRecyclable(true);
                }
            });
            mItemManger.bind(viewHolder.itemView, position);//实现只展现一条列表项的侧滑区域
            vh.tvContentTitle.setText(item.getSwitchName());
            vh.tvContentDescription.setText(item.getSwitchActionName());
            vh.itemView.setTag(item);
        }

        @Override
        public int getItemCount() {
            return dataList.size();
        }

        @Override
        public int getSwipeLayoutResourceId(int position) {
            return R.id.swipe_layout;
        }

        private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_delete:
                        SwitchBindBean bean = (SwitchBindBean) v.getTag();
                        callback.delete(bean);
                        break;
                }
            }
        };

        private final Context context;
        private final List<SwitchBindBean> dataList;
        private final Callback callback;

        public SwitchItemAdapter(Context context, List<SwitchBindBean> dataList, Callback callback) {
            this.context = context;
            this.dataList = dataList;
            this.callback = callback;
        }

        class ItemViewHolder extends RecyclerView.ViewHolder {

            public SwipeLayout swipeLayout;
            public ImageView btnDelete;
            public TextView tvContentTitle;
            public TextView tvContentDescription;

            public ItemViewHolder(@NonNull @NotNull View itemView) {
                super(itemView);
                swipeLayout = itemView.findViewById(R.id.swipe_layout);
                btnDelete = itemView.findViewById(R.id.btn_delete);
                tvContentTitle = itemView.findViewById(R.id.tv_content_title);
                tvContentDescription = itemView.findViewById(R.id.tv_content_description);
            }
        }
    }

    public interface Callback {
        void delete(SwitchBindBean bean);
    }
}
